package com.example.jsontest;

import com.example.db.DBHelper;

import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;

public class AlbumProvider extends ContentProvider {
	private DBHelper dbHelper = null;
	private static final UriMatcher uriMatcher = new UriMatcher(
			UriMatcher.NO_MATCH);
	private static final int ALBUM = 1;
	private static final int ALBUMS = 2;
	static {
		uriMatcher.addURI("com.example.jsontest.AlbumProvider", "album", ALBUMS);
		uriMatcher.addURI("com.example.jsontest.AlbumProvider", "album/#", ALBUM);
	}

	public AlbumProvider() {

	}

	@Override
	public boolean onCreate() {
		dbHelper = new DBHelper(getContext());
		return true;
	}

	@Override
	public Cursor query(Uri uri, String[] projection, String selection,
			String[] selectionArgs, String sortOrder) {
		int flg = uriMatcher.match(uri);
		SQLiteDatabase db = dbHelper.getReadableDatabase();
		Cursor cursor = null;
		switch(flg) {
		case ALBUM:
			long id = ContentUris.parseId(uri);
			String whereClause = " id = " + id;
			if(selection != null && !selection.isEmpty()) {
				whereClause += " and " + selection;
			}
			cursor = db.query("albums", null, whereClause, selectionArgs, null, null, null);
			break;
		case ALBUMS:
			cursor = db.query("albums", null, selection, selectionArgs, null, null, null);
			break;
		}
		return cursor;
	}

	@Override
	public String getType(Uri uri) {
		int flg = uriMatcher.match(uri);
		switch(flg) {
		case ALBUM:
			return "vnd.android.cursor.item/album";
		case ALBUMS:
			return "vnd.android.cursor.dir/album";
		}
		return null;
	}

	@Override
	public Uri insert(Uri uri, ContentValues values) {
		int flg = uriMatcher.match(uri);
		Uri resultUri = null;
		switch(flg) {
		case ALBUM:
			break;
		case ALBUMS:
			SQLiteDatabase db = dbHelper.getWritableDatabase();
			long id = db.insert("albums", null, values);
			resultUri = ContentUris.withAppendedId(uri, id);
			break;
		}
		return resultUri;
	}

	@Override
	public int delete(Uri uri, String selection, String[] selectionArgs) {
		int flg = uriMatcher.match(uri);
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		int effectNum = 0;
		switch(flg) {
		case ALBUM:
			long id = ContentUris.parseId(uri);
			String whereClause = " id = " + id;
			if(selection != null && !selection.isEmpty()) {
				whereClause += " and " + selection;
			}
			effectNum = db.delete("albums", whereClause, selectionArgs);
			break;
		case ALBUMS:
			effectNum = db.delete("albums", selection, selectionArgs);
			break;
		}
		return effectNum;
	}

	@Override
	public int update(Uri uri, ContentValues values, String selection,
			String[] selectionArgs) {
		// TODO Auto-generated method stub
		int flg = uriMatcher.match(uri);
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		int effectNum = 0;
		switch(flg) {
		case ALBUM:
			long id = ContentUris.parseId(uri);
			String whereClause = " id = " + id;
			if(selection != null && !selection.isEmpty()) {
				whereClause += " and " + selection;
			}
			effectNum = db.update("albums", values, whereClause, selectionArgs);
			break;
		case ALBUMS:
			//effectNum = db.update("albums", values, selection, selectionArgs);
			break;
		}
		return effectNum;
	}

}
